#include "stdafx.h"
#include "TGRxImplDictionary.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

TGRX_IMPLEMENT_DYNCREATE(TGRxImplDictionary,TGRxDictionary,MDLCURRENTVER,0,	\
						 _T("TGRxKernel.dll|") TGRX_CLASS_APP)

TGRxImplDictionary::TGRxImplDictionary()
{
	
}

TGRxImplDictionary::~TGRxImplDictionary()
{
	deletesObjects();
}

TGRxObject*	TGRxImplDictionary::at(const char* pszKey) const
{
	if (pszKey == NULL) return NULL;
	TGRxObject * pRxObject = NULL;
	m_mapName2RxClass.Lookup(pszKey,(void *&)pRxObject);
	return pRxObject;
}

TGRxObject*	TGRxImplDictionary::atPut(const char* pszKey, TGRxObject* pObject)
{
	if (pszKey == NULL || pObject == NULL) return NULL;
	TGRxObject * pRxObject = NULL;
	if (m_mapName2RxClass.Lookup(pszKey,(void *&)pRxObject))
	{
		return pRxObject;
	}

	m_mapName2RxClass.SetAt(pszKey,pObject);
	ASSERT(m_mapName2RxClass.Lookup(pszKey, (void*&)pRxObject));

	return NULL;
}

POSITION TGRxImplDictionary::getStartPosition() const
{
	return m_mapName2RxClass.GetStartPosition();
}

void TGRxImplDictionary::getNextAssoc(POSITION& rNextPosition, CString& sKey, void*& rValue) const
{
	m_mapName2RxClass.GetNextAssoc(rNextPosition,sKey,rValue);
}

BOOL TGRxImplDictionary::resetpszKey(const char* pszOldKey, const char* pszNewKey)
{
	if (pszOldKey == NULL || pszNewKey == NULL) return FALSE;
	TGRxObject*	pRxObject = at(pszOldKey);
	if (pRxObject == NULL)
		return FALSE;

	m_mapName2RxClass.RemoveKey(pszOldKey);
	m_mapName2RxClass.SetAt(pszNewKey,(void *)pRxObject);

	return TRUE;
}

TGRxObject*	TGRxImplDictionary::remove(const char* pszKey)
{
	if (pszKey == NULL) return NULL;
	TGRxObject*	pRxObject = at(pszKey);
	m_mapName2RxClass.RemoveKey(pszKey);
	return pRxObject;
}

BOOL TGRxImplDictionary::has(const char* pszEntryName) const
{
	if (pszEntryName == NULL) return FALSE;
	TGRxObject*	pRxObject = at(pszEntryName);
	return pRxObject != NULL;
}

long TGRxImplDictionary::numEntries() const
{
	return m_mapName2RxClass.GetCount();
}

BOOL TGRxImplDictionary::deletesObjects()
{
	CString sKey;
	TGRxObject * pRxObject = NULL;
	for (POSITION pos = m_mapName2RxClass.GetStartPosition();pos != NULL;) 
	{
		pRxObject = NULL;
		m_mapName2RxClass.GetNextAssoc(pos,sKey,(void *&)pRxObject);
		if (pRxObject != NULL)
		{
			delete pRxObject;
		}
	}
	m_mapName2RxClass.RemoveAll();
	return TRUE;
}
